resource "google_compute_network" "default" {
  name                    = "<%= ctx[:vars]['network_name'] %>"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "default" {
  name          = "<%= ctx[:vars]['subnet_name'] %>"
  ip_cidr_range = "10.0.1.0/24"
  region        = "us-central1"
  network       = google_compute_network.default.id
}

resource "google_compute_health_check" "hc" {
  name               = "<%= ctx[:vars]['health_check_name'] %>"
  check_interval_sec = 1
  timeout_sec        = 1

  tcp_health_check {
    port = "80"
  }
}

resource "google_compute_region_backend_service" "backend" {
  name          = "<%= ctx[:vars]['backend_name'] %>"
  region        = "us-central1"
  health_checks = [google_compute_health_check.hc.id]
}

resource "google_compute_forwarding_rule" "default" {
  name     = "<%= ctx[:vars]['forwarding_rule_name'] %>"
  region   = "us-central1"

  load_balancing_scheme = "INTERNAL"
  backend_service       = google_compute_region_backend_service.backend.id
  all_ports             = true
  network               = google_compute_network.default.name
  subnetwork            = google_compute_subnetwork.default.name
}

resource "google_compute_route" "<%= ctx[:primary_resource_id] %>" {
  name         = "<%= ctx[:vars]['route_name'] %>"
  dest_range   = "0.0.0.0/0"
  network      = google_compute_network.default.name
  next_hop_ilb = google_compute_forwarding_rule.default.id
  priority     = 2000
}
